﻿using System;

namespace a
{
    public abstract class Shape     //抽象类Shape
    {
        private string myType;

        public Shape(string s)
        {
            myType = s;
        }
        public abstract double Area
        {
            get;
        }
        public abstract bool isLegal();

    }

    public class Squre : Shape      //正方形类
    {
        private double mySide;
        private string myName;
        public Squre(double side, string type) : base(type)
        {
            mySide = side;
            myName = type;
        }
        public override double Area
        {
            get { return mySide * mySide; }
        }
        public override bool isLegal()
        {
            if (mySide <= 0)
            {
                return false;
            }
            else return true;
        }
        public override string ToString()
        {
            return myName + " 的边长为：" + mySide;
        }
    }

    public class Rectangle : Shape      //长方形类
    {
        private double myLength, myWidth;
        private string myName;
        public Rectangle(double length, double width, string type) : base(type)
        {
            myLength = length;
            myWidth = width;
            myName = type;
        }
        public override double Area
        {
            get { return myLength * myWidth; }
        }
        public override bool isLegal()
        {
            if (myLength <= 0 || myWidth <= 0)
            {
                return false;
            }

            else return true;
        }
        public override string ToString()
        {
            return myName + " 的长为：" + myLength + " 宽为：" + myWidth;
        }
    }
    public class Circle : Shape     //圆形类
    {
        private double myRadius;
        private string myName;
        public Circle(double radius, string type) : base(type)
        {
            myRadius = radius;
            myName = type;
        }
        public override double Area
        {
            get { return myRadius * myRadius * 3.14; }
        }
        public override bool isLegal()
        {
            if (myRadius <= 0)
            {
                return false;
            }
            else return true;
        }
        public override string ToString()
        {
            return myName + " 的半径为：" + myRadius;
        }
    }

    public class Test
    {
        public static void Main()
        {
            int num = 1;
            double totalArea = 0;
            string[] types = { "正方形", "长方形", "圆形" };
            Shape[] shapes = new Shape[10];
            Random random = new Random();
            while (num <= 10)
            {
                string type = types[random.Next(0, 3)];
                switch (type)
                {
                    case "正方形":
                        {
                            double side = Math.Round(random.NextDouble() * 100 - 50, 1);
                            Squre squre = new Squre(side, "正方形" + num);
                            if (squre.isLegal())
                            {
                                System.Console.WriteLine(squre + "  ");
                                totalArea += squre.Area;
                                shapes[num - 1] = squre;
                            }
                            else
                            {
                                System.Console.WriteLine(squre + "  " + "非正方形");
                                num--;
                            }

                        }; break;
                    case "长方形":
                        {
                            double length = Math.Round(random.NextDouble() * 100 - 50, 1);
                            double width = Math.Round(random.NextDouble() * 100 - 50, 1);
                            Rectangle rec = new Rectangle(length, width, "长方形" + num);
                            if (rec.isLegal())
                            {
                                System.Console.WriteLine(rec + "  ");
                                totalArea += rec.Area;
                                shapes[num - 1] = rec;
                            }
                            else
                            {
                                System.Console.WriteLine(rec + "  " + "非长方形");
                                num--;
                            }
                        }; break;
                    case "圆形":
                        {
                            double radius = Math.Round(random.NextDouble() * 100 - 50, 1);
                            Circle circle = new Circle(radius, "圆形" + num);
                            if (circle.isLegal())
                            {
                                System.Console.WriteLine(circle + "  ");
                                totalArea += circle.Area;
                                shapes[num - 1] = circle;
                            }
                            else
                            {
                                System.Console.WriteLine(circle + "  " + "非圆形");
                                num--;
                            }
                        }; break;
                    default:; break;
                }
                num++;
            }
            System.Console.WriteLine("");
            System.Console.WriteLine("最终十个图形为：");
            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine(shapes[i]);
            }
            System.Console.WriteLine("");
            System.Console.WriteLine("总面积为：" + totalArea);
        }
    }
}